<?php
namespace app\admin\controller;
use think\facade\Db;
use app\admin\model\RegionModel;
use PhpOffice\PhpSpreadsheet\IOFactory;

class CityRegionData  {
    public function setRegionData() {
        $json_string = file_get_contents(public_path().'static/3700_region.json');
        $data = json_decode($json_string,true);  
        print_r($data);
        //$this->inputCube($data);
    }

    private function inputCube($arr) {
        foreach($arr as $value) {
            $params = array(
                'code'=>$value['code'],
                'nameZH'=>$value['nameZH'],
                'nameEN'=>$value['nameEN'],
            );
            if($value['parentId']) {                
                $params['pcode']=$value['parentId'];
            }
            Db::table('yhq_3700_region')->insert($params);
            if(isset($value['sonList'])) {
                $this->inputCube($value['sonList']);
            }
        }
    }

    public function setIndciatorData() {
        // $json_string = file_get_contents(public_path().'static/3700_datax14.json');
        // $data = json_decode($json_string,true);  
        // foreach($data['table']['values'] as $key=>$item) {
        //     if($key>0) {
        //         foreach($item as $k=>$i) {
        //             if($k>1) {
        //                 $indicatorKey = floor(($key-1)/18);
        //                 $indicatorCode = $data['indicators'][$indicatorKey];
        //                 $regionKey = fmod(floatval(($key-1)),18);
        //                 $regionCode = $data['regions'][$regionKey];
        //                 $timeCode = $data['times'][$k-2];
        //                 $arr = array(
        //                     'indicator_code'=>$indicatorCode,
        //                     'region_code'=>$regionCode,
        //                     'time_code'=>$timeCode,
        //                     'value'=>$i['value']
        //                 );
        //                 Db::table('yhq_3700_data')->insert($arr);
        //             }
        //         }
        //     }
        // }
        
        // echo '结束了';
    }

    public function setTimeData() {
        $json_string = file_get_contents(public_path().'static/3700_time.json');
        $data = json_decode($json_string,true);  
        var_dump($data);
        foreach($data as $value) {
            $params = array(
                'code'=>$value['code'],
                'nameZH'=>$value['nameZH'],
            );
            Db::table('yhq_3700_time')->insert($params);
        }
    }

    public function getTimeCode() {
        $list = Db::table('yhq_3700_time')->select();
        return $list;
    }

    public function getRegionCode() {
        $list = Db::table('yhq_3700_region')->where(array('pcode'=>'01'))->select();
        return $list;
    }

    public function getIndicatorCode() {
        $list = Db::table('yhq_3700_indicator')->where(array('isVal'=>1))->select();
        return $list;
    }
}